#include "mapobject.h"
#include "geometry.h"

#include <limits>

Polygon::Polygon(const std::vector<QPointF> &pts, MapObject *owner) 
: points(pts)
{
	mapObject = owner;
}

double min ( double a, double b ) 
{ 
	if ( a < b )
		return a;
	else
		return b;
}

double max ( double a, double b ) { return a > b ? a : b; }

QRectF Polygon::boundingRect()
{
	double veryBigNumber = 10000.0;
    if ( bounds.isEmpty ())
    {    
		bounds.setLeft   (  veryBigNumber );
		bounds.setTop    (  veryBigNumber );
        bounds.setRight  ( -veryBigNumber );
		bounds.setBottom ( -veryBigNumber );

        for ( std::vector<QPointF>::const_iterator it = points.begin(); it != points.end(); ++it )
        {
            bounds.setLeft   ( min ( bounds.topLeft().x(),     it -> x ()));
            bounds.setTop    ( min ( bounds.topLeft().y(),     it -> y ()));
            bounds.setRight  ( max ( bounds.bottomRight().x(), it -> x ()));
            bounds.setBottom ( max ( bounds.bottomRight().y(), it -> y ()));
        }
    }
    return bounds;
}

Polygon::~Polygon()
{
}

QPolygonF Polygon::toQPolygon() const
{
    QPolygonF polygon;
    for ( std::vector<QPointF>::const_iterator it = points.begin(); it != points.end(); ++it )
    {
        polygon << QPointF ( it -> x (), it -> y ());
    }
    return polygon;
}

Polyline::Polyline(const std::vector<QPointF> &pts, MapObject *owner) 
: points(pts)
{
	mapObject = owner;
}

QPolygonF Polyline::toQPolygon() const
{
    QPolygonF polygon;
    for ( std::vector<QPointF>::const_iterator it = points.begin(); it != points.end(); ++it )
    {
        polygon << QPointF ( it -> x (), it -> y ());
    }
    return polygon;
}

QRectF Polyline::boundingRect()
{
	double veryBigNumber = 10000.0;
    if ( bounds.isEmpty ())
    {    
		bounds.setLeft   (  veryBigNumber );
		bounds.setTop    (  veryBigNumber );
        bounds.setRight  ( -veryBigNumber );
		bounds.setBottom ( -veryBigNumber );

        for ( std::vector<QPointF>::const_iterator it = points.begin(); it != points.end(); ++it )
        {
            bounds.setLeft   ( min ( bounds.topLeft().x(),     it -> x ()));
            bounds.setTop    ( min ( bounds.topLeft().y(),     it -> y ()));
            bounds.setRight  ( max ( bounds.bottomRight().x(), it -> x ()));
            bounds.setBottom ( max ( bounds.bottomRight().y(), it -> y ()));
        }
    }
    return bounds;
}

Polyline::~Polyline()
{
}
